using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace MoEngine
{
    public class Core
    {
        protected static GameCore m_GameInst = null;


        public static GameCore Game
        {
            get { return m_GameInst; }
            set { m_GameInst = value; }
        }

        protected CoreComponentCollection m_Components = new CoreComponentCollection();

        protected CoreComponentCollection Components
        {
            get { return m_Components; }
        }

        public static ClockManager ClockMgr;
        public static WindowsManager WndMgr;
        public static ContentManager Content;
        public static InputManager InputMgr;

        public Core(IServiceProvider services)
        {
            m_GlobalServices = services;

            Components.Add(ClockMgr = new ClockManager());
            Components.Add(InputMgr = new InputManager());
            Components.Add(WndMgr = new WindowsManager());
        }

        protected static IServiceProvider m_GlobalServices;

        protected static IGraphicsDeviceService m_GlobalGraphicsService;

        public static GraphicsDevice GlobalDevice
        {
            get { return m_GlobalGraphicsService.GraphicsDevice; }
        }

        public static Random m_Random = new Random();

        public static Random Random
        {
            get { return m_Random; }
        }

        /// <summary>
        /// Initialize
        /// </summary>
        public virtual void Initialize()
        {
            Content = new ContentManager(m_GlobalServices, "Content");
            m_GlobalGraphicsService = (IGraphicsDeviceService)m_GlobalServices.GetService(typeof(IGraphicsDeviceService));

            foreach (CoreComponent component in Components.UpdateList)
            {
                if (component.Enabled)
                    component.Initialize();
            }
        }

        /// <summary>
        /// Update
        /// </summary>
        /// <param name="appTime"></param>
        public virtual void Update(GameTime appTime)
        {
            foreach (CoreComponent component in Components.UpdateList)
            {
                if (component.Enabled)
                    component.Update(appTime);
            }
        }

        /// <summary>
        /// Draw
        /// </summary>
        /// <param name="appTime">App time</param>
        public virtual void Draw(GameTime appTime)
        {
            foreach (CoreComponentDrawable component in Components.DrawList)
            {
                if (component.Visible)
                    component.Draw(appTime);
            }

        }
    }
}
